<?php
require_once("class/config.php");
if(isset($_COOKIE['username'])){
 $user = $_COOKIE['username']; 
   echo('<center>You are already logged in!</center>');
   }else{
   if(!isset($_POST['login']) || empty($_POST['login'])){
	print('<link rel="stylesheet" href="css/style.css" type="text/css">
	<center>
	<form action="" method="post">
	<div class="login_text">Username:</div> <input type="text" name="username" class="login_box"><br>
	<div class="login_text">Password:</div> <input type="password" name="password" class="login_box"><br>
	<input type="submit" name ="login" value="Login" class="login_button"><br>
	No account? Then click <span><a href="register.php">here</a></span>!
	</center>
	</form>');
	}elseif($_POST['login'] == 'Login'){
         
      $password = $_POST['password'];
      $username = $_POST['username'];

	  $query = mysql_query("SELECT user, password FROM login WHERE user = '$username'");
	  $check = mysql_num_rows($query);
	  
	  $row = mysql_fetch_object($query);
	  $hash = md5($password);
	  
	  if($check == 0){
	  echo ('<center>No user with this informations!</center>');
	  }else{
      if($username == $row->user && $hash == $row->password){
	   setcookie("username", $username, time()+3600);
	   setcookie("password", md5($hash), time()+3600);
       echo('<center>Logged in!</center>');
	  }else{
	   echo('<center>Wrong informations!<br>');
	   echo('Do you have got forgotten your password?<br>');
	   echo('Then click <span><a href="password.php">here</a></span>!</center>');
     }
    }
  }
}
?>